#include "buzzer.h"
#include "delay.h"
#include "midi.h"

void BUZZER_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Pin = BUZZER;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(BUZZER_PORT,&GPIO_InitStructure);
	
	GPIO_WriteBit(BUZZER_PORT, BUZZER, (BitAction)(1));//蜂鸣器接口高电平,避免长时间工作烧坏
}

void BUZZER_Beep1(void)
{
	u16 i;
	for(i = 0; i < 200; i ++){
		GPIO_WriteBit(BUZZER_PORT,BUZZER,(BitAction)(0));
		delay_us(500);
		GPIO_WriteBit(BUZZER_PORT,BUZZER,(BitAction)(1));
		delay_us(500);
	}
}

void BUZZER_Play()
{
	u16 i,e;
	for(i = 0; i < 39; i++){
		for(e = 0; e < music_demo[i*2] * music_demo[i*2+1]/1000;e++){
			GPIO_WriteBit(BUZZER_PORT,BUZZER,(BitAction)(0));
			delay_us(500000/music_demo[i*2]);
			GPIO_WriteBit(BUZZER_PORT,BUZZER,(BitAction)(1));
			delay_us(500000/music_demo[i*2]);
		}
	}
}


void BUZZER_Play_GOT()
{
	u16 i,e;
	for(i = 0; i < sizeof(Game_of_Thrones)/sizeof(Game_of_Thrones[0])/2; i++){
		for(e = 0; e < Game_of_Thrones[i*2] * Game_of_Thrones[i*2+1]/1000;e++){
			GPIO_WriteBit(BUZZER_PORT,BUZZER,(BitAction)(0));
			delay_us(240000/Game_of_Thrones[i*2]);
			GPIO_WriteBit(BUZZER_PORT,BUZZER,(BitAction)(1));
			delay_us(240000/Game_of_Thrones[i*2]);
		}
	}
}
